home *** CD-ROM | disk | FTP | other *** search
- VERSION 2.00
- Begin Form frmHQ
- Caption = "Headquarters"
- ClientHeight = 5385
- ClientLeft = 2055
- ClientTop = 1455
- ClientWidth = 2490
- Height = 5790
- Left = 1995
- LinkTopic = "Form1"
- ScaleHeight = 5385
- ScaleWidth = 2490
- Top = 1110
- Width = 2610
- Begin CommandButton Command7
- Caption = "End"
- Height = 495
- Left = 600
- TabIndex = 6
- Top = 4320
- Width = 1215
- End
- Begin CommandButton Command6
- Caption = "Color All Pictures #2"
- Height = 495
- Left = 600
- TabIndex = 5
- Top = 3600
- Width = 1215
- End
- Begin CommandButton Command5
- Caption = "Cross Hatch"
- Height = 495
- Left = 600
- TabIndex = 4
- Top = 3000
- Width = 1215
- End
- Begin CommandButton Command4
- Caption = "Color Specific Picture"
- Height = 495
- Left = 600
- TabIndex = 3
- Top = 2400
- Width = 1215
- End
- Begin CommandButton Command3
- Caption = "Color All Pictures #1"
- Height = 495
- Left = 600
- TabIndex = 2
- Top = 1680
- Width = 1215
- End
- Begin CommandButton Command2
- Caption = "Position Controls"
- Height = 495
- Left = 600
- TabIndex = 1
- Top = 960
- Width = 1215
- End
- Begin CommandButton Command1
- Caption = "Load other forms."
- Height = 495
- Left = 600
- TabIndex = 0
- Top = 240
- Width = 1215
- End
- Option Explicit
- Const CMD_COUNT = 7
- Const FORMS_COUNT = 3
- Const PIC_COUNT = 6
- 'Create an array of Form references.
- Dim MyForms(FORMS_COUNT) As frmForm1
- 'Create an array of Picture references for the picture
- 'boxes from the other 3 forms.
- Dim MyPictures(PIC_COUNT) As PictureBox
- 'Create an array of references to command buttons
- Dim MyCommands(CMD_COUNT) As commandbutton
- 'The previous declaration could be made more generic like this.
- 'Dim MyCommands(CMD_COUNT) As control
- Sub Command1_Click ()
- Dim i As Integer
- 'Declare a specific form variable
- Dim ThisForm As frmForm1
- 'Or, the preceeding variable could have been generic.
- 'Specific object variables are more efficient.
- 'Dim ThisForm as Form
- 'Initialize the array of form1 references.
- 'Note that there's an implicit Load statement in a Set.
- 'First, refer to the form obtained at design time.
- Set MyForms(1) = frmForm1
- 'Create 2 additional copies of form1, and 'point' at them.
- Dim form2 As New frmForm1
- Set MyForms(2) = form2
- Dim form3 As New frmForm1
- Set MyForms(3) = form3
-
- 'Position the forms, set their tags, and show them.
- 'Use of ThisForm as a 'pointer' simplifies the statements.
- For i = 1 To FORMS_COUNT
- Set ThisForm = MyForms(i)
- ThisForm.Width = screen.Width * .25
- ThisForm.Height = screen.Height
- ThisForm.Top = 0
- ThisForm.Left = i * .25 * screen.Width
- ThisForm.Caption = "Form #" & i
- 'Each form's tag becomes a number.
- ThisForm.Tag = Str$(i)
- ThisForm.Show
- Next i
- 'Here's another way of doing it. Notice how not using a reference
- 'variable makes the individual statements longer.
- 'For i = 2 To FORMS_COUNT
- ' MyForms(i).Width = screen.Width * .25
- ' MyForms(i).Height = screen.Height
- ' MyForms(i).Top = 0
- ' MyForms(i).Left = i * .25 * screen.Width
- ' MyForms(i).Caption = "Type #" & i
- 'Each form's tag becomes a number.
- ' MyForms(i).Tag = Str$(i)
- ' MyForms(i).Show
- 'Next i
- 'Disable this control, and enable the next one.
- command1.Enabled = False
- Command2.Enabled = True
- End Sub
- Sub Command2_Click ()
- Dim i As Integer
- 'Initialize the array of picture references. The beauty of this is
- 'that it permits treating all controls from all forms in a program
- 'with a single array.
- 'The complicated expression in the 2 SET statements maps
- '1 to 1 and 2
- '2 to 3 and 4
- '3 to 5 and 6, etc.
- 'The ! is the preferred separator for a form and its controls.
- 'Thus, use form!control but form.property and form.method
- For i = 1 To FORMS_COUNT
- Set MyPictures(2 * i - 1) = MyForms(i)!picP1
- Set MyPictures(2 * i) = MyForms(i)!picP2
- Next i
-
- 'Position the picture controls.
- For i = 1 To FORMS_COUNT
- CenterPicture MyForms(i), MyPictures(2 * i - 1), UPPER
- CenterPicture MyForms(i), MyPictures(2 * i), LOWER
- Next i
- Command2.Enabled = False
- For i = 3 To CMD_COUNT - 1
- MyCommands(i).Enabled = True
- Next i
- End Sub
- Sub Command3_Click ()
- Dim i As Integer
- For i = 1 To PIC_COUNT
- SetColor MyPictures(i)
- Next i
- End Sub
- Sub Command4_Click ()
- Dim rc As Integer
- rc = Val(InputBox$("Enter a number between 1 and 6", "Change Color"))
- If (rc < 1 Or rc > 6) Then
- Exit Sub
- End If
- SetColor MyPictures(rc)
- End Sub
- Sub Command5_Click ()
- Dim i As Integer
- Dim rc As Integer
- Dim x1 As Single, y2 As Single
- 'The specific object variable TP will be used as an alias to
- 'simplify the code. This is most useful when a control with
- 'a lengthy name will be used a lot in a given subroutine.
- Dim TP As PictureBox
- 'Note that TP could have been a generic object variable, though
- 'this is NOT as efficient.
- 'Dim TP As control
- rc = Val(InputBox$("Enter a number between 1 and 6", "Change Color"))
- If (rc < 1 Or rc > 6) Then
- Exit Sub
- End If
- 'Point to the requested picture control
- Set TP = MyPictures(rc)
- 'Clear the specific picture box by setting its
- 'background color to white.
- TP.BackColor = RGB(255, 255, 255)
- 'Draw horizontal and vertical lines.
- For i = 1 To 10
- y2 = i * .1 * TP.Height
- TP.Line (0, y2)-(TP.Width, y2)
-
- x1 = i * .1 * TP.Width
- TP.Line (x1, 0)-(x1, TP.Height)
- Next i
- 'Notice how ugly and lengthy the statements are when an object
- 'variable is NOT used to refer to the requested picture control.
- 'For i = 1 To 10
- ' y2 = i * .1 * MyPictures(rc).Height
- ' MyPictures(rc).Line (0, y2)-(MyPictures(rc).Width, y2)
- '
- ' x1 = i * .1 * MyPictures(rc).Width
- ' MyPictures(rc).Line (x1, 0)-(x1, MyPictures(rc).Height)
- 'Next i
- End Sub
- Sub Command6_Click ()
- Dim i As Integer, j As Integer
- 'Color all pictures boxes using the forms collection
- 'and each form's controls collection.
- For i = 0 To forms.Count - 1
- If TypeOf forms(i) Is frmForm1 Then
- For j = 0 To forms(i).Controls.Count - 1
- SetColor forms(i).Controls(j)
- Next j
- End If
- Next i
- End Sub
- Sub Command7_Click ()
- Unload frmHQ
- End Sub
- Sub Form_Load ()
- Const DELTA1 = 250
- Const DELTA2 = 900
- Const DELTA3 = 250
- Dim i As Integer
- 'This form should occupy the left 1/4 of the screen.
- Top = 0
- Left = 0
- Height = screen.Height
- Width = screen.Width * .25
- 'The 6 command buttons on this form are placed
- 'in an array of controls.
- Set MyCommands(1) = command1
- Set MyCommands(2) = Command2
- Set MyCommands(3) = Command3
- Set MyCommands(4) = Command4
- Set MyCommands(5) = Command5
- Set MyCommands(6) = Command6
- Set MyCommands(7) = Command7
- 'Disable buttons 2-6.
- For i = 2 To CMD_COUNT - 1
- MyCommands(i).Enabled = False
- Next i
- 'Set every command button's width appropriate to its caption.
- For i = 1 To CMD_COUNT
- MyCommands(i).Width = TextWidth(MyCommands(i).Caption) + DELTA3
- Next i
- MyCommands(1).Top = DELTA1
- MyCommands(1).Left = (ScaleWidth - MyCommands(1).Width) / 2
- 'After positioning the first, position the others relative to it.
- For i = 2 To CMD_COUNT
- MyCommands(i).Left = (ScaleWidth - MyCommands(i).Width) / 2
- MyCommands(i).Top = MyCommands(i - 1).Top + DELTA2
- Next i
- 'Finally, seed the random number generator.
- Randomize Timer
- End Sub
- Sub Form_Unload (Cancel As Integer)
- Dim i As Integer
- 'Unload the forms in reverse sequence
- If command1.Enabled = False Then
- For i = FORMS_COUNT To 1 Step -1
- Unload MyForms(i)
- Next i
- End If
- End Sub
-